package com.wlyy.his.service.ums.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlyy.his.common.util.enumUtil.idcard.IDCardUtil;
import com.wlyy.his.entity.ums.AccountInfo;
import com.wlyy.his.entity.ums.Certificate;
import com.wlyy.his.bean.user.pc.vo.AccountInfoVo;
import com.wlyy.his.bean.user.pc.vo.CertificateVo;
import com.wlyy.his.mapper.ums.AccountInfoMapper;
import com.wlyy.his.mapper.ums.CertificateMapper;
import com.wlyy.his.service.ums.IAccountInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 账户信息 服务实现类
 * </p>
 *
 * @author ZhouYanhui
 * @since 2021-08-31
 */
@Service
public class AccountInfoServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo> implements IAccountInfoService {

    @Resource
    private AccountInfoMapper accountInfoMapper;
    @Resource
    private CertificateMapper certificateMapper;
    @Override
    public AccountInfoVo getAccountInfo(String accountId) {
        AccountInfoVo userInfoVo = new AccountInfoVo();
        //查询用户信息
        AccountInfo accountInfo = accountInfoMapper.selectOne(new QueryWrapper<AccountInfo>()
                .eq("account_id", accountId));
        if (null!=accountInfo){
            BeanUtil.copyProperties(accountInfo,userInfoVo);
        }
        //查询证件信息
        List<Certificate> certificates = certificateMapper.selectList(new QueryWrapper<Certificate>()
                .eq("account_id", accountId));
        if (BeanUtil.isNotEmpty(certificates)){
            //证件信息
            List<CertificateVo> certificateVos = BeanUtil.copyToList(certificates, CertificateVo.class);
            userInfoVo.setCertificateVos(certificateVos);
            //获取有身份证的证件信息
            Certificate certificate = certificates.stream().filter(item -> item.getCertType().equals(0)).findFirst().get();
            //根据身份证计算年龄
            Integer age = IDCardUtil.getAge(certificate.getCertNo());
            userInfoVo.setAge(age);
            userInfoVo.setCertificateVos(certificateVos);
        }

        return userInfoVo;
    }

}
